package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.maiji.cloud.entities.shopingmall.*;
import com.maiji.cloud.mapper.ParamConfigMapper;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.shopingmall.ParamConfigReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.shopingmall.ParamConfigResData;
import com.maiji.cloud.service.*;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@Transactional
public class ParamConfigServiceImpl extends ServiceImpl<ParamConfigMapper, ParamConfigEntity> implements ParamConfigService {

    @Autowired
    private GoodsParamConfigService goodsParamConfigService;

    @Override
    public BaseDataResDto<List<ParamConfigResData>> editParamConfigs(List<ParamConfigReqData> data) {
        Map<Boolean, List<ParamConfigReqData>> booleanListMap = data.parallelStream().collect(Collectors.groupingBy(pcrd -> StringUtil.isNotBlank(pcrd.getUuId())));
        List<ParamConfigEntity> paramConfigList = Lists.newArrayList();
        List<ParamConfigReqData> updList = booleanListMap.get(true);
        if (updList != null) {
            List<ParamConfigEntity> paramConfigEntities = updList.parallelStream().map(pcrd -> {
                ParamConfigEntity paramConfigEntity = new ParamConfigEntity();
                BeanUtils.copyProperties(pcrd, paramConfigEntity);
                return paramConfigEntity.setIsDel(0);
            }).collect(Collectors.toList());
            if (! updateBatchById(paramConfigEntities)) return new BaseDataResDto(Status.ERROR);
            paramConfigList.addAll(paramConfigEntities);
        }
        List<ParamConfigReqData> insertList = booleanListMap.get(false);
        if (insertList != null) {
            List<ParamConfigEntity> paramConfigEntities = insertList.parallelStream().map(pcrd -> {
                ParamConfigEntity paramConfigEntity = new ParamConfigEntity();
                BeanUtils.copyProperties(pcrd, paramConfigEntity);
                return paramConfigEntity.setUuId(UUID_MD5.getUUID()).setIsDel(0);
            }).collect(Collectors.toList());
            if (! insertBatch(paramConfigEntities)) return new BaseDataResDto(Status.ERROR);
            paramConfigList.addAll(paramConfigEntities);
        }
        List<ParamConfigResData> resData = paramConfigList.parallelStream().map(pce -> {
            ParamConfigResData paramConfigResData = new ParamConfigResData();
            BeanUtils.copyProperties(pce, paramConfigResData);
            return paramConfigResData;
        }).sorted().collect(Collectors.toList());
        return new BaseDataResDto<List<ParamConfigResData>>(Status.SUCCESS).setData(resData);
    }

    @Override
    public BaseResDto delParamConfig(String pcId) {
        if (! deleteById(pcId)) return new BaseResDto(Status.ERROR);
        EntityWrapper<GoodsParamConfigEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("param_config_id", pcId);
        if (! goodsParamConfigService.delete(entityWrapper)) return new BaseResDto(Status.ERROR);
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseDataResDto<List<ParamConfigResData>> getParamConfigByGoodsId(String goodsId) {
        Wrapper<GoodsParamConfigEntity> wrapper = new EntityWrapper<GoodsParamConfigEntity>().eq("goods_id", goodsId);
        List<GoodsParamConfigEntity> goodsParamConfigList = goodsParamConfigService.selectList(wrapper);
        Map<String,GoodsParamConfigEntity> map = goodsParamConfigList.stream().collect(Collectors.toMap(GoodsParamConfigEntity::getParamConfigId, gpc -> gpc));
        EntityWrapper<ParamConfigEntity> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("is_del", 0);
        entityWrapper.orderBy("sort", true);
        List<ParamConfigEntity>  list = selectList(entityWrapper);
        List<ParamConfigResData> listParamConfigResData = list.stream().map(paramConfigEntity ->{
            ParamConfigResData paramConfigResData = new ParamConfigResData();
            BeanUtils.copyProperties(paramConfigEntity, paramConfigResData);
            GoodsParamConfigEntity goodsParamConfig = map.get(paramConfigEntity.getUuId());
            if(goodsParamConfig != null) paramConfigResData.setValue(goodsParamConfig.getValue());
            return paramConfigResData;
        }).collect(Collectors.toList());
        return new BaseDataResDto<List<ParamConfigResData>>(Status.SUCCESS).setData(listParamConfigResData);
    }

}
